#include "urlutils.h"
#include <QtCore>

QString UrlUtils::toLocalFile(QUrl url) {
//    return url.toLocalFile();         // This will work soon...

    QString retval = url.toString();    // normally you don't do this.

    // strip off file://
    static QRegExp rx("^\\w{4,5}://(.*)$");
    int pos = rx.indexIn(retval);
    if (pos != -1) retval = rx.cap(1);

    // check for drive letters, strip off leading /
    static QRegExp rx2("^/([a-zA-Z]:/.*$)");
    pos = rx2.indexIn(retval);
    if (pos != -1) retval = rx2.cap(1);

    return retval;
}

QString UrlUtils::baseName(QUrl url) {
    QString path = toLocalFile(url);
    return baseName(path);
}

QString UrlUtils::baseName(QString path) {
    QFileInfo fi(path);
    return baseName(fi);
}

QString UrlUtils::baseName(QFileInfo fi) {
    return fi.completeBaseName();
/*  QString retval = fi.fileName();
    int idx = retval.lastIndexOf('.');
    if (idx != -1) return retval.left(idx);
    else return retval; */
}

